<template>
    <div id="myChart" :style="{width: '1300px', height: '2000px'}">
    </div>
</template>
<script>

    import axios from 'axios'
    export default {
        name: 'Echarts',
        mounted(){
            let _this=this

            axios.get('http://localhost:8888/getLabelTop').then(function (response) {
                // 基于准备好的dom，初始化echarts实例
                let myChart = _this.$echarts.init(document.getElementById('myChart'))
                // 绘制图表
                myChart.setOption({
                    title: {
                        text: '2021年每期每周必看播放量最高视频展示',
                        subtext: '数据来自bilibili'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // Use axis to trigger tooltip
                            type: 'shadow'        // 'shadow' as default; can also be 'line' or 'shadow'
                        }
                    },
                    legend: {
                        data: ['投币数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'value'
                    },
                    yAxis: {
                        type: 'category',
                        data: response.data.labelTitle
                    },
                    series: [
                        {
                            name: '播放数',
                            type: 'bar',
                            stack: 'total',
                            label: {
                                show: true
                            },
                            emphasis: {
                                focus: 'series'
                            },
                            data: response.data.view,
                            itemStyle: {
                                normal: {
                                    //这里是重点
                                    color: function(params) {
                                        //注意，如果颜色太少的话，后面颜色不会自动循环，最好多定义几个颜色
                                        var colorList = ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83', '#ca8622'];
                                        var index;
                                        //给大于颜色数量的柱体添加循环颜色的判断
                                        if (params.dataIndex >= colorList.length) {
                                            index = params.dataIndex - colorList.length;
                                            return colorList[index];
                                        }
                                        return colorList[params.dataIndex];
                                    }
                                }
                            }
                        }
                    ]
                });
            })

        }
    }
</script>